<?php

//$_GET['page_size'] 每页多少条
//$_GET['page'] 第几页

function pager_get_page_size() {
//获得每页多少条
	$m_size=get_to_int($_GET['page_size']);
	if($m_size<=0)$m_size=10;
	return $m_size;
}

function pager_get_page() {
//获取页号
	$m_page=get_to_int($_GET['page']);
	if($m_page<=0)$m_page=1;
	return $m_page;
}

function url_append_param($url,$param) {
	if(strstr($url,'?'))return "$url&$param";
	return "$url?$param";
}

class Pager {
	var $size;  //每页列出多少
	var $page;  //第几页
	var $count; //实际列出的数量(手动赋值
	
	function Pager() {
		$this->size = pager_get_page_size();
		$this->page = pager_get_page();
		$this->count = 0;
	}
	
	function mysql() { //limit子句
		$m_start=($this->page-1)*($this->size);
		return " limit $m_start,{$this->size} ";
	}
	function start() { //编号从0开始的本页起始编号
		return ($this->page-1)*($this->size);
	}
	function current_no() { //编号从0开始的当前编号
		return ($this->page-1)*($this->size)+$this->count;
	}
	function url() {
		return "page_size={$this->size}&page={$this->page}";
	}
	function top_url() {
		return "page_size={$this->size}";
	}
	function delta_url($page_delta) {
		$m_page=$this->page+$page_delta;
		if($m_page<=0)$m_page=1;
		return "page_size={$this->size}&page=$m_page";
	}
	//html for knowledge
	function html_top_page($base_url) {
		$url=url_append_param($base_url,$this->top_url());
		return "[<a href='$url'>" . _d('page_top') . '</a>]&nbsp;';
	}
	function html_prev_page($base_url) {
		if($this->page>1) {
			$url=url_append_param($base_url,$this->delta_url(-1));
			return "[<a href='$url'>" . _d('page_prev') . '</a>]&nbsp;';
		} else {
			return '[' . _d('page_prev') . ']&nbsp;';
		}
	}
	function html_next_page($base_url) {
		if($this->count==$this->size) {
			$url=url_append_param($base_url,$this->delta_url(1));
			return "[<a href='$url'>" . _d('page_next') . '</a>]&nbsp;';
		} else {
			return '[' . _d('page_next') . ']&nbsp;';
		}
	}
}

?>
